package com.transcargo.request;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.transcargo.action.ServletParametersException;
import com.transcargo.action.ServletValidationException;
import com.transcargo.db.dbException;
import com.transcargo.geo.GeoObjectNotFoundException;
import com.transcargo.mail.Mail;
import com.transcargo.rpc.RPCOutput;
import com.transcargo.users.User;
import com.transcargo.users.UserNotFoundException;
import com.transcargo.util.Normalizer;

public class GetMailRequest extends AuthRequest {
	private String xml = "";
	private String id = "";
	private int Id = 0;
	
    private void servletGetValues(HttpServletRequest request) throws ServletParametersException {
    	id = request.getParameter("id");
    	if (id == null) id = "";
    }
    private void servletValidateValues() throws ServletValidationException {
    	if (id.length() == 0 || Integer.parseInt(id) == 0) throw new ServletValidationException("Empty id");
    	Id = Integer.parseInt(id);
    }
    private void servletExecuteRequest() throws ServletRequestException, ServletParametersException {
    	Mail mail = null;
    	try {
   			list = dbase.execute("SELECT mail.sender_id, mail.recipient_id, mail.date, mail.priority, mail.subject, mail.seen, mail_body.body FROM mail, mail_body WHERE mail.body_id=mail_body.id AND mail.id=" + Id + " AND owner=" + User_Id);
    		iter = list.iterator();
    		if (iter.hasNext()) {
    			String[] res = iter.next();
    			mail = new Mail();
    			mail.id = Id;
    			mail.sender = new User(Integer.parseInt(res[0]), dbase);
    			mail.recipient = new User(Integer.parseInt(res[1]), dbase);
    			mail.date = Normalizer.DenormalizeTime(res[2]);
    			mail.priority = res[3];
    			mail.subject = res[4];
    			mail.read = res[5].equals("1");
    			mail.body = res[6];
    		} else throw new ServletParametersException();
    		if (mail.recipient.getId() == User_Id)
    			xml =  RPCOutput.generate(mail,"folder","inbox");
    		else
    			xml =  RPCOutput.generate(mail,"folder","outbox");
    		if (mail.recipient.getId() == User_Id && !mail.read) dbase.executeUpdate("UPDATE mail SET seen=1 WHERE id=" + Id);
    	} catch (GeoObjectNotFoundException e) {
    		throw new ServletRequestException();
    	} catch (UserNotFoundException e) {
    		throw new ServletRequestException();
		} catch (dbException e) {
    		throw new ServletRequestException();
		}
    }
    public void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        PrintWriter out = null;
		try {
            out = response.getWriter();
            servletGetValues(request);
            servletValidateValues();
            servletExecuteRequest();
            out.println(xml);
	    } catch (ServletParametersException e) {
           sendRedirect(response, "mail.jsp");
        }  catch (ServletValidationException e) {
            sendError(response, new Exception(e.getMessage()));
        }  catch (ServletRequestException e) {
            String xml =  RPCOutput.generate(false);
            out.println(xml);
        }  catch (IOException e) {
            System.out.println("IOException: " + e.getMessage());
        }
    }

}
